/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: ModelTaskController.java
 * Author:   13075787
 * Date:     2016年7月11日 上午9:40:30
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.web.controller.model;

import java.security.Principal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.nsfuaa.employee.dto.EmployeeInfo;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.model.ModelCommodityBaseInfo;
import com.suning.sawp.intf.model.ModelConstant;
import com.suning.sawp.intf.model.ModelTaskHomeFlagVo;
import com.suning.sawp.intf.model.SubmitAssignReqList;
import com.suning.sawp.intf.model.TaskCateVo;
import com.suning.sawp.intf.model.TaskInfoVo;
import com.suning.sawp.intf.model.vo.ModelAssignDetailResponse;
import com.suning.sawp.intf.model.vo.ModelAssignTaskResponse;
import com.suning.sawp.intf.model.vo.ModelInventoryDetailResponse;
import com.suning.sawp.intf.model.vo.ModelInventoryTaskResponse;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.service.impl.model.ModelBlendServiceImpl;
import com.suning.sawp.service.impl.model.ModelCommodityInfoServiceImpl;
import com.suning.sawp.service.impl.model.ModelTaskNewServiceImpl;
import com.suning.sawp.service.impl.model.ModelTaskServiceImpl;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 样机管理<br>
 * 任务相关
 *
 * @author 13075787
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/model/task")
public class ModelTaskController extends DjBaseController {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ModelTaskController.class);
            
    @Autowired
    ModelBlendServiceImpl modelBlendService;

    @Autowired
    StoreManService storeManService;

    @Autowired
    ModelTaskServiceImpl modelTaskService;
    
    /**
     * 样机新处理服务
     */
    @Autowired
    ModelTaskNewServiceImpl modelTaskNewService;

    @Autowired
    ModelCommodityInfoServiceImpl modelCommodityInfoService;

    /**
     * 功能描述: <br>
     * 首页查询
     * 
     */
    @RequestMapping("/queryTaskHome.do")
    public MyGsonView queryTaskHome(Principal principal) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<ModelTaskHomeFlagVo> ret = modelTaskService.queryTaskHomeFlag(storeMan);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 查询任务列表，需要传入任务类型(盘点、簇维护)
     */
    @RequestMapping("/queryMyTasks.do")
    public MyGsonView queryMyTasks(Principal principal, Integer taskType) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<TaskInfoVo>> ret = modelTaskService.queryTaskList(storeMan, taskType);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 查询当前任务可分配的品类列表
     * 
     */
    @RequestMapping("/queryAssignedCates.do")
    public MyGsonView querfyAssignedCates(Principal principal, Long taskId) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<TaskCateVo>> ret = modelTaskService.queryAssignedCates(storeMan, taskId);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 查询该任务可分配的品牌列表
     */
    @RequestMapping("/queryAssignedBrands.do")
    public MyGsonView queryAssignedBrands(Principal principal, Long taskId) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<TaskCateVo>> ret = modelTaskService.queryAssignedBranchs(storeMan, taskId);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 分配品类提交
     *
     * @param principal
     * @param assignReqList
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/submitAssignCateTask.do")
    public MyGsonView submitAssignCateTask(Principal principal, @RequestBody SubmitAssignReqList assignReqList) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<TaskCateVo>> ret = modelTaskService.submitAssignCateTask(storeMan,
                    assignReqList.getAssignList());
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 分配品牌提交
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/submitAssignBrandTask.do")
    public MyGsonView submitAssignBrandTask(Principal principal, @RequestBody SubmitAssignReqList assignReqList) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<TaskCateVo>> ret = modelTaskService.submitAssignBrandTask(storeMan,
                    assignReqList.getAssignList());
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 查询可分配的人员列表
     */
    @RequestMapping("/queryAssignedEmployee.do")
    public MyGsonView queryAssignedEmployee(Principal principal) {
        MyGsonView view = new MyGsonView();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if (modelBlendService.checkManInfo(storeMan)) {
            ReturnMsg<List<EmployeeInfo>> ret = modelTaskService.queryAssignedEmployee(storeMan);
            view = setGsonView(ret);
        } else {
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        return view;
    }

    /**
     * 功能描述: <br>
     * 检查商品有效性
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping("/queryCommValidity.do")
    public MyGsonView queryCommValidity(String commCode) {
        MyGsonView view = null;
        ModelCommodityBaseInfo commInfo = modelCommodityInfoService.queryCommodityBaseInfo(commCode);
        if (null != commInfo && StringUtils.isNotBlank(commInfo.getCommName())) {
            ReturnMsg ret = new ReturnMsg();
            ret.setRetFlag(ReturnMsg.SUCCESS);
            view = setGsonView(ret);
        } else {
            ReturnMsg ret = new ReturnMsg();
            ret.setError(ModelConstant.MODEL_ERROR_NO_COMM,
                    ModelConstant.getErrorValue(ModelConstant.MODEL_ERROR_NO_COMM));
            view = setGsonView(ret);
        }
        return view;
    }

    /**
     * 查询分配任务列表
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryMyAssignTasks.do")
    public MyGsonView queryMyAssignTasks(Principal principal, Integer currIndex, Integer pageSize) {
        LOGGER.info("queryMyAssignTasks enter,staffId={}", principal.getName());
        MyGsonView view = new MyGsonView();
        // 返回对象
        ReturnMsg<ModelAssignTaskResponse> ret = new ReturnMsg<ModelAssignTaskResponse>();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        // 校验员工信息是否有缺失
        if (modelBlendService.checkManInfo(storeMan)) {
            // 查询分配列表服务
            ModelAssignTaskResponse resp = modelTaskNewService.queryMyAssignTasks(storeMan,
                    DJStringUtils.initCurrIndex(currIndex), DJStringUtils.initPageSize(pageSize));
            ret.setData(resp);
            LOGGER.debug("queryMyAssignTasks resp={}", resp);
            view = setGsonView(ret);
        } else {
            LOGGER.error("queryMyAssignTasks error, storeMan={}", storeMan);
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        LOGGER.info("queryMyAssignTasks exit,staffId={}", principal.getName());
        return view;
    }
    
    /**
     * 查询分配任务详情
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param taskId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryMyAssignTaskDetail.do")
    public MyGsonView queryMyAssignTaskDetail(Principal principal, String taskId) {
        LOGGER.info("queryMyAssignTaskDetail enter,staffId={},taskId={}", principal.getName(), taskId);
        MyGsonView view = new MyGsonView();
        if (StringUtils.isBlank(taskId)) {
            LOGGER.warn("queryMyAssignTaskDetail taskId is null");
            setErrorMsg(view, ErrorCodeConstants.ERR_0003, "taskId");
        } else {
            // 返回对象
            ReturnMsg<ModelAssignDetailResponse> ret = new ReturnMsg<ModelAssignDetailResponse>();
            // 工号
            String staffId = principal.getName();
            StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
            // 校验员工信息是否有缺失
            if (modelBlendService.checkManInfo(storeMan)) {
                // 查询详情服务
                ModelAssignDetailResponse resp = modelTaskNewService.queryMyAssignTaskDetail(storeMan, 
                        Long.valueOf(taskId));
                LOGGER.debug("queryMyAssignTaskDetail resp={}", resp);
                ret.setData(resp);
                view = setGsonView(ret);
            } else {
                LOGGER.error("queryMyAssignTaskDetail error, storeMan={}", storeMan);
                setErrorMsg(view, ErrorCodeConstants.ERR_1009);
            }
        }
        LOGGER.info("queryMyAssignTaskDetail exit,staffId={},taskId={}", principal.getName(), taskId);
        return view;
    }
    
    /**
     * 查询盘点任务列表
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param taskType
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryMyInventoryTasks.do")
    public MyGsonView queryMyInventoryTasks(Principal principal, Integer currIndex, Integer pageSize) {
        LOGGER.info("queryMyInventoryTasks enter,staffId={}", principal.getName());
        MyGsonView view = new MyGsonView();
        // 返回对象
        ReturnMsg<ModelInventoryTaskResponse> ret = new ReturnMsg<ModelInventoryTaskResponse>();
        // 工号
        String staffId = principal.getName();
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        // 校验员工信息是否有缺失
        if (modelBlendService.checkManInfo(storeMan)) {
            // 查询盘点列表服务
            ModelInventoryTaskResponse resp = modelTaskNewService.queryMyInventoryTasks(storeMan,
                    DJStringUtils.initCurrIndex(currIndex), DJStringUtils.initPageSize(pageSize));
            LOGGER.debug("queryMyInventoryTasks resp={}", resp);
            ret.setData(resp);
            view = setGsonView(ret);
        } else {
            LOGGER.error("queryMyInventoryTasks error, storeMan={}", storeMan);
            setErrorMsg(view, ErrorCodeConstants.ERR_1009);
        }
        LOGGER.info("queryMyInventoryTasks exit,staffId={}", principal.getName());
        return view;
    }
    
    /**
     * 查询盘点任务详情
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param principal
     * @param taskId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryMyInventoryTaskDetail.do")
    public MyGsonView queryMyInventoryTaskDetail(Principal principal, String taskId) {
        LOGGER.info("queryMyInventoryTaskDetail enter,staffId={},taskId={}", principal.getName(), taskId);
        MyGsonView view = new MyGsonView();
        if (StringUtils.isBlank(taskId)) {
            LOGGER.warn("queryMyInventoryTaskDetail taskId is null");
            setErrorMsg(view, ErrorCodeConstants.ERR_0003, "taskId");
        } else {
            // 返回对象
            ReturnMsg<ModelInventoryDetailResponse> ret = new ReturnMsg<ModelInventoryDetailResponse>();
            // 工号
            String staffId = principal.getName();
            StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
            // 校验员工信息是否有缺失
            if (modelBlendService.checkManInfo(storeMan)) {
                // 查询详情服务
                ModelInventoryDetailResponse resp = modelTaskNewService.queryMyInventoryTaskDetail(storeMan, 
                        Long.valueOf(taskId));
                LOGGER.debug("queryMyInventoryTaskDetail resp={}", resp);
                ret.setData(resp);
                view = setGsonView(ret);
            } else {
                LOGGER.error("queryMyInventoryTaskDetail error, storeMan={}", storeMan);
                setErrorMsg(view, ErrorCodeConstants.ERR_1009);
            }
        }
        LOGGER.info("queryMyInventoryTaskDetail exit,staffId={},taskId={}", principal.getName(), taskId);
        return view;
    }
}
